//
//  UIView+YJCategory.m
//  Rail
//
//  Created by hychen on 2018/2/26.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "UIView+YJCategory.h"

@implementation UIView (YJCategory)
#pragma mark - 手势
// 添加单击手势
- (void)addTapGestureWithTarget:(id)target action:(SEL)action
{
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    [self addGestureRecognizer:tapGesture];
}

// 添加长按手势
- (void)addLongPressGestureWithTarget:(id)target action:(SEL)action minDuration:(NSTimeInterval)minDuration {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:target action:action];
    longPressGesture.minimumPressDuration = minDuration;
    [self addGestureRecognizer:longPressGesture];
}

// 添加滑动手势
- (void)addPanGestureWithTarget:(id)target action:(SEL)action
{
    self.userInteractionEnabled = YES;
    UIPanGestureRecognizer *tapGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:action];
    [self addGestureRecognizer:tapGesture];
}

#pragma mark - 截图
// 截图并生成UIImageView
- (UIImageView *)shotImageView
{
    CGSize imageSize = self.frame.size;
    UIImage *image = [self shotImage];
    
    // 创建
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = image;
    imageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
    return imageView;
}

// 截图
- (UIImage *)shotImage
{
    CGSize imageSize = self.frame.size;
    UIGraphicsBeginImageContext(imageSize);
    // 渲染
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    [[self layer] renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - 圆角
// 变圆
- (void)toCircle
{
    CGFloat corner = self.frame.size.height * 0.5;
    [self setCorner:corner];
}

// 设置圆角
- (void)setCorner:(CGFloat)corner
{
    self.layer.cornerRadius = corner;
    self.clipsToBounds = YES;
    [self.layer setMasksToBounds:YES];
}

#pragma mark - 其它
// 删除所有子控件
- (void)removeAllSubviews
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
}
@end
